local _G = _G
local addon, ns = ...
local profile = ns.profile
local cfg = {}
local LabelUI
local noshow
local configframe
if not _G.LabelUI_mod then
	_G.LabelUI_mod = {}	
end
local Sharedmediamod = ns.Sharedmediamod
  
local Auroramod = CreateFrame("Frame", nil, UIParent)

Auroramod.additions = {}

local function styleconfig(confframe)
	if not configframe then
		configframe = confframe
	end
	if Auroramod.ready then
		if Auroramod.additions["ConfigFrame"] then
			Auroramod.additions["ConfigFrame"] = nil
		end
		local Aurora = _G.Aurora 
		local F = Aurora[1]
		if not configframe.styled then
			F.CreateBD(configframe)
			--F.CreateSD(configframe)
			F.Reskin(configframe.cancelbtn)
			F.Reskin(configframe.okbtn)
			configframe.styled = true
		end
		for i, v in pairs(configframe.frames) do
			if v.type == "button" then
				F.Reskin(v)	
			elseif v.type == "dropdown" then
				F.ReskinDropDown(v)	
			elseif v.type == "checkbox" then
				F.ReskinCheck(v)	
			end
		end
	else
		Auroramod.additions["ConfigFrame"] = styleconfig	
	end
end
  
local function Enablemod()
	local Aurora = _G.Aurora 
	local alpha = AuroraConfig.alpha
	Aurora[1].CreateBD = function(f, a, b) 
		f:SetBackdrop({
			bgFile = Aurora[2].media.backdrop,
			edgeFile = Aurora[2].media.backdrop,
			edgeSize = 1,
		})
		f:SetBackdropColor(0, 0, 0, a or alpha)
		f:SetBackdropBorderColor(0, 0, 0, b or 1)
		if not a then tinsert(Aurora[2].frames, f) end
	end		
	Aurora[1].CreateBD(_G["BNToastFrame"], 0.5, 0)
	local tooltips = {
		"GameTooltip",
		"ItemRefTooltip",
		"ShoppingTooltip1",
		"ShoppingTooltip2",
		--"ShoppingTooltip3",
		"WorldMapTooltip",
		"ChatMenu",
		"EmoteMenu",
		"LanguageMenu",
		"VoiceMacroMenu",
	}
		
	for i = 1, #tooltips do
		local t = _G[tooltips[i]]
		if t:GetChildren() then
			local kids = { t:GetChildren() }
			for _, bg in ipairs(kids) do
				if bg and bg.GetBackdrop then
					local file = bg:GetBackdrop()
					if file then 
						file = file["bgFile"]
						if file == Aurora[2].media.backdrop then 
							Aurora[1].CreateBD(bg, .5, 0)
						end
					end
				end
			end
		end
	end
	Auroramod.ready = true --we update core func
	for i,v in pairs(Auroramod.additions) do  --simple call back
		v()			
	end
end 

Auroramod:SetScript("OnEvent", function(self, event, arg1)
		local ctime = GetTime() + 1.5
		Auroramod:SetScript("OnUpdate", function()
			if GetTime() > ctime then
				Enablemod()
				Auroramod:SetScript("OnUpdate", noshow)
			end		
		end)
		Auroramod:UnregisterEvent("PLAYER_ENTERING_WORLD")
end)
  
local function Init()
	_, _, _, enabled, _, reason = GetAddOnInfo("Aurora")
	if reason == "MISSING" or not enabled then return end
	LabelUI.StyleCallBack["Aurora"] = styleconfig
	Auroramod:RegisterEvent("PLAYER_ENTERING_WORLD") --happend after "PLAYER_LOGIN" (Init func start at it)
end

function Auroramod.InitMod(main, profilename)
	if not profile[profilename] then
		for i,v in pairs(profile) do
			if type(i) == "string" then
				profilename = i
				break
			end
		end
	end
	for i,v in pairs(profile[profilename].aurora) do
		cfg[i] = v		
	end
	noshow = main.noshow 
	LabelUI = main
	main:AddInit(Init, "Addon AuroraMod")
end 

ns.Auroramod = Auroramod
  
_G.LabelUI_mod["Auroramod"] = Auroramod